<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const isCollapse = ref(false);
const activeMenu = ref('');
const menuData = ref([]);
const userInfo = ref({
  name: '',
  role: { name: '' },
  department: { name: '' }
});

// 处理菜单点击
const handleMenuClick = (menu) => {
  if (menu.url) {
    router.push(menu.url);
  }
};

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('userid');
  localStorage.removeItem('permissions');
  localStorage.removeItem('userInfo');
  ElMessage.success('退出登录成功');
  router.push('/');
};

// 处理折叠菜单
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value;
};

// 将权限数据转换为菜单结构
const transformPermissionsToMenu = (permissions) => {
  // 找出所有父级菜单
  const parentMenus = permissions.filter(item => item.pid === null);
  
  // 为每个父菜单找到对应的子菜单
  const result = parentMenus.map(parent => {
    const children = permissions.filter(item => item.pid === parent.id);
    return {
      id: parent.id,
      title: parent.name,
      url: parent.url,
      icon: getIconByName(parent.name),
      children: children.map(child => ({
        id: child.id,
        title: child.name,
        url: child.url,
        icon: getIconByName(child.name)
      }))
    };
  });
  
  return result;
};

// 根据菜单名称返回对应的图标
const getIconByName = (name) => {
  const iconMap = {
    '医生管理': 'UserFilled',
    '医生列表': 'List',
    '医生排班': 'Calendar',
    '预约管理': 'Calendar',
    '患者管理': 'User',
    '药房管理': 'FirstAidKit',
    '默认': 'Menu'
  };
  
  return iconMap[name] || iconMap['默认'];
};

onMounted(() => {
  // 获取存储的权限数据
  const permissionsStr = localStorage.getItem('permissions');
  if (permissionsStr) {
    const permissions = JSON.parse(permissionsStr);
    menuData.value = transformPermissionsToMenu(permissions);
  }
  
  // 获取用户信息
  const userInfoStr = localStorage.getItem('userInfo');
  if (userInfoStr) {
    userInfo.value = JSON.parse(userInfoStr);
  }
  
  // 默认选中第一个菜单，但不自动跳转
  if (menuData.value.length > 0) {
    const firstMenu = menuData.value[0];
    if (firstMenu.children && firstMenu.children.length > 0) {
      activeMenu.value = firstMenu.children[0].url;
    } else {
      activeMenu.value = firstMenu.url;
    }
  }
});
</script>

<template>
  <div class="layout-container">
    <!-- 侧边栏 -->
    <div class="sidebar" :class="{ 'is-collapsed': isCollapse }">
      <div class="logo-container">
        <img src="https://via.placeholder.com/40" alt="Logo" class="logo" />
        <span v-if="!isCollapse" class="logo-text">七星医疗系统</span>
      </div>
      
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          background-color="#001529"
          text-color="#fff"
          active-text-color="#1890ff"
          unique-opened
          router
        >
          <template v-for="menu in menuData" :key="menu.id">
            <!-- 有子菜单的情况 -->
            <el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.url || menu.id.toString()">
              <template #title>
                <el-icon>
                  <component :is="menu.icon"></component>
                </el-icon>
                <span>{{ menu.title }}</span>
              </template>
              
              <el-menu-item 
                v-for="subMenu in menu.children" 
                :key="subMenu.id" 
                :index="subMenu.url"
                @click="handleMenuClick(subMenu)"
              >
                <el-icon>
                  <component :is="subMenu.icon"></component>
                </el-icon>
                <span>{{ subMenu.title }}</span>
              </el-menu-item>
            </el-sub-menu>
            
            <!-- 无子菜单的情况 -->
            <el-menu-item 
              v-else 
              :index="menu.url"
              @click="handleMenuClick(menu)"
            >
              <el-icon>
                <component :is="menu.icon"></component>
              </el-icon>
              <span>{{ menu.title }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-scrollbar>
    </div>
    
    <!-- 主内容区 -->
    <div class="main-container">
      <!-- 顶部导航栏 -->
      <div class="header">
        <div class="header-left">
          <el-icon class="collapse-btn" @click="toggleCollapse">
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/about' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>数据总览</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click">
            <div class="user-info">
              <el-avatar size="small" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
              <span class="username">{{ userInfo.name }}</span>
              <el-icon><CaretBottom /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <span>{{ userInfo.role?.name || '未知角色' }}</span>
                </el-dropdown-item>
                <el-dropdown-item>
                  <span>{{ userInfo.department?.name || '未知部门' }}</span>
                </el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">
                  <el-icon><SwitchButton /></el-icon>
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      
      <!-- 内容区域 -->
      <div class="content">
        <div class="dashboard">
          <h2>数据总览</h2>
          
          <!-- 数据卡片 -->
          <div class="data-cards">
            <el-card class="data-card">
              <div class="data-card-content">
                <div class="data-card-value">500</div>
                <div class="data-card-title">今日预约人数</div>
              </div>
              <el-icon class="data-card-icon blue"><Calendar /></el-icon>
            </el-card>
            
            <el-card class="data-card">
              <div class="data-card-content">
                <div class="data-card-value">400</div>
                <div class="data-card-title">今日接诊人数</div>
              </div>
              <el-icon class="data-card-icon green"><User /></el-icon>
            </el-card>
            
            <el-card class="data-card">
              <div class="data-card-content">
                <div class="data-card-value">50000</div>
                <div class="data-card-title">今日门诊总金额</div>
              </div>
              <el-icon class="data-card-icon orange"><Money /></el-icon>
            </el-card>
            
            <el-card class="data-card">
              <div class="data-card-content">
                <div class="data-card-value">40000</div>
                <div class="data-card-title">今日门诊总人数</div>
              </div>
              <el-icon class="data-card-icon purple"><PieChart /></el-icon>
            </el-card>
          </div>
          
          <!-- 图表区域 -->
          <div class="chart-container">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-card class="chart-card">
                  <template #header>
                    <div class="chart-header">
                      <span>近7日挂号预约情况</span>
                    </div>
                  </template>
                  <div class="chart-placeholder">
                    <div class="chart-mock-bar">
                      <div class="bar" style="height: 60%;"></div>
                      <div class="bar" style="height: 80%;"></div>
                      <div class="bar" style="height: 40%;"></div>
                      <div class="bar" style="height: 55%;"></div>
                      <div class="bar" style="height: 90%;"></div>
                      <div class="bar" style="height: 65%;"></div>
                      <div class="bar" style="height: 75%;"></div>
                    </div>
                  </div>
                </el-card>
              </el-col>
              
              <el-col :span="12">
                <el-card class="chart-card">
                  <template #header>
                    <div class="chart-header">
                      <span>近7日门诊充值趋势</span>
                    </div>
                  </template>
                  <div class="chart-placeholder">
                    <div class="chart-mock-line">
                      <div class="point" style="bottom: 20%;"></div>
                      <div class="point" style="bottom: 30%;"></div>
                      <div class="point" style="bottom: 60%;"></div>
                      <div class="point" style="bottom: 80%;"></div>
                      <div class="point" style="bottom: 50%;"></div>
                      <div class="point" style="bottom: 60%;"></div>
                      <div class="point" style="bottom: 40%;"></div>
                    </div>
                  </div>
                </el-card>
              </el-col>
            </el-row>
          </div>
          
          <!-- 表格区域 -->
          <el-card class="table-card">
            <template #header>
              <div class="table-header">
                <span>实时挂号</span>
              </div>
            </template>
            <el-table :data="[]" style="width: 100%" empty-text="暂无数据">
              <el-table-column prop="name" label="姓名" width="120" />
              <el-table-column prop="department" label="科室" width="120" />
              <el-table-column prop="doctor" label="医生" width="120" />
              <el-table-column prop="time" label="挂号时间" width="180" />
              <el-table-column prop="status" label="状态" />
              <el-table-column label="操作">
                <template #default>
                  <el-button size="small" type="primary">查看</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.layout-container {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 220px;
  height: 100%;
  background-color: #001529;
  transition: width 0.3s;
  overflow: hidden;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  z-index: 10;
}

.sidebar.is-collapsed {
  width: 64px;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  overflow: hidden;
  background-color: #002140;
}

.logo {
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

.logo-text {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  margin-left: 12px;
  white-space: nowrap;
}

.sidebar-menu {
  border-right: none;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
  margin-right: 16px;
}

.header-right {
    display: flex;
    align-items: center;
  }

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 8px;
  color: #333;
}

.content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f0f2f5;
}

.dashboard h2 {
  margin-bottom: 24px;
  font-weight: 500;
  color: #333;
}

.data-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.data-card {
  position: relative;
  height: 120px;
  display: flex;
  align-items: center;
  padding: 20px;
}

.data-card-content {
  flex: 1;
}

.data-card-value {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 10px;
}

.data-card-title {
  font-size: 14px;
  color: #999;
}

.data-card-icon {
  font-size: 48px;
  opacity: 0.2;
}

.blue {
  color: #1890ff;
}

.green {
  color: #52c41a;
}

.orange {
  color: #fa8c16;
}

.purple {
  color: #722ed1;
}

.chart-container {
  margin-bottom: 20px;
}

.chart-card {
  margin-bottom: 20px;
  height: 350px;
}

.chart-header {
  font-weight: 500;
}

.table-card {
  margin-bottom: 20px;
}

.table-header {
  font-weight: 500;
}

/* 模拟图表样式 */
.chart-placeholder {
  height: 280px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.chart-mock-bar {
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  width: 100%;
  height: 100%;
  padding: 20px;
}

.chart-mock-bar .bar {
  width: 40px;
  background: linear-gradient(to top, #1890ff, #69c0ff);
  border-radius: 4px 4px 0 0;
}

.chart-mock-line {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 20px;
}

.chart-mock-line .point {
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: #1890ff;
  border-radius: 50%;
}

.chart-mock-line .point:nth-child(1) {
  left: 10%;
}

.chart-mock-line .point:nth-child(2) {
  left: 25%;
}

.chart-mock-line .point:nth-child(3) {
  left: 40%;
}

.chart-mock-line .point:nth-child(4) {
  left: 55%;
}

.chart-mock-line .point:nth-child(5) {
  left: 70%;
}

.chart-mock-line .point:nth-child(6) {
  left: 85%;
}

.chart-mock-line .point:nth-child(7) {
  left: 95%;
}

.chart-mock-line::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 10%;
  width: 85%;
  height: 2px;
  background-color: #1890ff;
  z-index: -1;
}
</style>
